package com.serkol.abmanager.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.serkol.abmanager.bean.Result;
import com.serkol.abmanager.component.TokenAdminUtils;
import com.serkol.abmanager.config.ConstantHeader;
import com.serkol.abmanager.constant.ResultConstant;
import com.serkol.abmanager.entity.AdminToken;
import com.serkol.abmanager.entity.Users;
import com.serkol.abmanager.service.UsersService;
import org.apache.commons.codec.digest.DigestUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api/login")
@ResponseBody
public class LoginApiController {

    @Autowired
    private UsersService usersService;

    @Autowired
    private TokenAdminUtils tokenAdminUtils;

    @PostMapping("login")
    public Result login(HttpSession httpSession, @RequestParam(name = "name", defaultValue = "") String name, @RequestParam(name = "password", defaultValue = "") String password) {

        Result result = new Result();
        if (!name.isEmpty() && !password.isEmpty()) {
            QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
            String encode = DigestUtils.md5Hex(password);
            queryWrapper.lambda().eq(Users::getUserName, name).eq(Users::getUserPass, encode);
            Users one = usersService.getOne(queryWrapper);
            System.out.println(one);
            if (one != null) {
                System.out.println(one);
                AdminToken adminToken = new AdminToken();
                adminToken.setIp(ConstantHeader.getIp());
                adminToken.setTerminal(ConstantHeader.getTerminal());
                adminToken.setCreateDate(new Date());
                adminToken.setName(one.getName());
                adminToken.setUserName(one.getUserName());
                adminToken.setUserId(one.getId());
                String token = tokenAdminUtils.getToken(adminToken);

                Map<String, Object> map = new HashMap<>();
                map.put("name", one.getName());
                map.put("rfid", one.getRfid());
                map.put("tell", one.getTell());
                map.put("email", one.getEmail());
                map.put("userName", one.getUserName());
                map.put("roleId", one.getRoleId());
                map.put("groupId", one.getGroupId());
                map.put("groupType", one.getGroupType());
                map.put("deptId", one.getDeptId());
                map.put("carId", one.getCarId());
                map.put("token", token);
                httpSession.setAttribute("token", token);
                result.setStatus(ResultConstant.SUCSESS);
                result.setData(map);
            } else {
                result.setStatus(ResultConstant.EMPTIY);
            }
        } else {
            result.setMsg("用户名或密码不能为空");
        }

        return result;
    }


    @PostMapping("/usererror")
    @ResponseBody
    public Result userError() {
        Result result = new Result();
        result.setStatus(ResultConstant.TOKEN);
        return result;
    }

}
